public class Solution2 {
    public ListNode removeNthFromEnd (ListNode head, int n) {
        // write code here
        // write code here
        int length = 0;
        ListNode p = head;
        ListNode q = head;
        // 获取链表的长度
        while(head != null){
            length++;
            head = head.next;
        }
        if(length < 2){
            return null;
        }
        // 特殊情况
        if(n == length){
            return q.next;
        }
        int i = 0;
        while(p != null){
            i++;
            if(i == length - n){
                // 删除结点
                p.next = p.next.next;
            }
            p = p.next;
        }
        return q;
    }
}
